<template>
  <div class="gold-shop-wrap">
    <van-nav-bar title="邀请好友" left-arrow fixed @click-left="onClickLeft" />
    <!-- 轮播图 -->
    <div class="swiper" id="swiper1">
      <div class="swiper-wrapper">
        <div
          class="swiper-slide"
          v-for="(item, index) in background"
          :key="index"
        >
          <div class="swiper_item" :id="'posterHtml' + index"> <!-- 修改为每个海报的唯一ID -->
            <img :src="item" class="bookImg" />
            <div class="slide_qrcode">
              <div class="qrcode-container">
                <img :src="invite.avatar_url" alt="" class="avatar" />
                <div>{{ invite.nickname }}</div>
              </div>
              <vue-qr class="last_img" :text="codeText"></vue-qr>
            </div>
          </div>
        </div>
      </div>
      <div class="swiper-pagination"></div>
    </div>
    <!-- 底部 -->
    <div class="invite_bom">
      <div class="invite_bom_item">
        <img src="../../images/mine/xiazai.png" alt="" @click="downLoadApp" />
        <div>下载</div>
      </div>
      <div class="invite_bom_item" @click="createPoster">
        <img src="../../images/mine/wechat.png" alt="" />
        <div>分享好友</div>
      </div>
    </div>
  </div>
</template>

<script>
import Swiper from "swiper";
// import 'swiper/css/pagination';
import { INVITE_CONFIG } from "@/api/mine";
import html2canvas from 'html2canvas';
import VueQr from "vue-qr";
export default {
  name: "HomeView",
  components: { VueQr },
  data() {
    return {
      invite: {},
      background: [],
      codeText: "",
    };
  },
  mounted() {
    this.$http.get(INVITE_CONFIG).then((res) => {
      this.invite = res.data;
      this.background = res.data.background_list;
      this.codeText = res.data.invite_url;
      this.$nextTick(() => {
        new Swiper("#swiper1", {
          direction: "horizontal",
          speed: 600,
          loop: true,
          autoplay: false,
          observer: true,
          observeParents: true,
          effect: "coverflow",
          slidesPerView: "auto",
          centeredSlides: true,
          coverflowEffect: {
            rotate: 0,
            stretch: 4,
            depth: 50,
            modifier: 3,
            slideShadows: false,
          },
          pagination: {
            el: ".swiper-pagination",
            clickable: true,
          },
        });
      });
    });
  },
  methods: {
    createPoster() {
      this.$toast("暂未开通")
      return
      const promises = this.background.map((item, index) => {
        const domObj = document.getElementById('posterHtml' + index); // 修改为每个海报的唯一ID
        return html2canvas(domObj, {
          useCORS: true,
          allowTaint: false,
          logging: false,
          letterRendering: true,
          onclone(doc) {
            let e = doc.querySelector('#posterHtml' + index);
            e.style.display = 'block';
          }
        }).then(function(canvas) {
          const link = document.createElement('a');
          link.href = canvas.toDataURL('image/png');
          link.download = '海报_' + index + '.png'; // 设置下载文件名
          link.click(); // 触发下载
        });
      });

      Promise.all(promises).then(() => {
        console.log('所有海报已生成并下载');
      });
    },
    onClickLeft() {
      this.$router.back();
    },
    // 下载
    downLoadApp() {
      window.open(this.codeText, '_blank');
    },
  },
};
</script>

<style lang="scss" scoped>
.gold-shop-wrap {
  padding-top: 46px;
  background: #f2f4f5;
  min-height: calc(100vh - 46px);
  ::v-deep .van-nav-bar .van-icon {
    color: #333333;
  }

  ::v-deep .van-nav-bar__arrow {
    font-size: 20px;
  }
}

.swiper {
  width: 100%;
  overflow: hidden;
  padding-top: 0.4rem;
  background: transparent;
  .swiper-slide {
    width: 85% !important;
    height: auto;
    text-align: center;
    position: relative;
    padding-bottom: 30px;
    background: transparent;
    .swiper_item {
      position: relative; /* 确保子元素定位相对于此 */
      background: transparent;
      .slide_qrcode {
        width: 80%;
        position: absolute;
        border-radius: 0.2rem;
        background: rgba(255, 255, 255, 1); /* 背景 */
        display: flex;
        justify-content: space-between;
        padding: 0.32rem;
        box-sizing: border-box;
        left: 50%;
        bottom: 10%; /* 调整底部位置 */
        z-index: 999;
        transform: translateX(-50%);
        align-items: center; /* 确保内容垂直居中 */

        .qrcode-container {
          display: flex;
          align-items: center; /* 确保头像和昵称垂直居中 */
          width: 80%;
          div{
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
          }
          .avatar {
            width: 0.72rem; /* 头像宽度 */
            height: 0.72rem; /* 头像高度 */
            border-radius: 50%; /* 圆形头像 */
            margin-right: 0.2rem; /* 头像与昵称之间的间距 */
          }
        }

        .last_img {
          width: 1.2rem;
          height: 1.2rem;
          margin-top: 0;
          display: block;
        }
      }
    }
  }

  .bookImg {
    width: 85% !important;
    height: auto;
  }

  .swiper-pagination {
    bottom: 10px;
  }
  ::v-deep .swiper-pagination-bullet-active{
    background: #F32A23 !important;
  }
}

.invite_bom {
  width: 100%;
  padding: 0.24rem;
  box-sizing: border-box;
  background: #fff;
  display: flex;
  justify-content: space-between;
  position: fixed;
  bottom: 0;

  &_item {
    width: 30%;
    text-align: center;
    font-family: PingFangSC, PingFang SC;
    font-weight: 400;
    font-size: 0.24rem;
    color: #333333;
    line-height: 0.34rem;
    font-style: normal;

    img {
      margin: 0 auto;
      width: 0.72rem;
      height: 0.72rem;
      margin-bottom: 0.12rem;
    }
  }
}
</style>
